<?php
/**
 * Validate
 * @Version 1.0
 * date: 14May2008
 * 
 * @author: Thanh NV
 * email: sony_k48@yahoo.com
 * 
 * @desc use for validate data
*/

class Validate {
	private $sErrorMsg = '';

	public function __construct(){
		$this->sErrorMsg = '';
	}
	
	public function reset(){
		$this->sErrorMsg = '';
	}
	
	public function getErrorMsg(){
		if($this->IsNotEmpty($this->sErrorMsg)){
			return $this->sErrorMsg;
		}
		else 
			return false;
	}
	
	public function check($val, $rule, $sErrorMsg)
	{
		$aRule=explode(",",$rule);

		foreach ($aRule as $rule) {
			switch ($rule)
			{
				case "IsEmail":
					$cond= $this->IsEmail($val);
					break;
				case "IsNumber":
					$cond= $this->IsNumber($val);
					break;
				case "IsNotEmpty":
					$cond= $this->IsNotEmpty($val);
					break;
				case "IsMoney":
					$cond= $this->IsMoney($val);
					break;
				case "IsPercent":
					$cond= $this->IsPercent($val);
					break;
				case "IsUSPhone":
					$cond= $this->IsUSPhone($val);
					break;
				case "IsASTPhone":
					$cond= $this->IsASTPhone($val);
					break;
				case "IsAreaCode":
					$cond= $this->IsAreaCode($val);
					break;
				case "IsPostCode":
					$cond= $this->IsPostCode($val);
					break;
				case "IsHTTP":
					$cond= $this->IsHTTP($val);
					break;
				case "IsIP":
					$cond= $this->IsIP($val);
					break;
				case "IsSelected":
					$cond= $this->IsSelected($val);
					break;
				case "IsColor":
					$cond= $this->IsColor($val);
					break;
				default:
					$cond=TRUE;
			}
			if (!$cond){
				$this->sErrorMsg .= "<br>- " . $sErrorMsg;
				return FALSE;
			}
		}
		return TRUE;
	}
	
	public function IsMatchString($sString1, $sString2, $sErrorMsg){
		if($sString1 != $sString2){
			$this->sErrorMsg .= "<br>- " . $sErrorMsg;
			return false;
		}
		return true;
	}
	public function IsGreaterNumber($iNumber1, $iNumber2, $sErrorMsg){
		if($iNumber1 < $iNumber2){
			$this->sErrorMsg .= "<br>- " . $sErrorMsg;
			return false;
		}
		return true;
	}
	
	public function IsChecked($oObject, $sErrorMsg){
		if(!isset($oObject)){
			$this->sErrorMsg .= "<br>- " . $sErrorMsg;
			return false;
		}
		return true;
	}

	//Form validation functions
	public function IsHTTP($addr)
	{
		$valid = true;
		if($addr!='http://' && strlen($addr)>0){
			if (!($addr[strlen($addr)-1] == "/")) $addr .= "/";
			$addr = str_replace(eregi_replace("^(.{2,6}://)?[^/]*/", "", $addr), "", $addr);
			if (!preg_match('/^((http|https|ftp):\/\/)?(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\//i', $addr))
			//if (!preg_match('/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\//i', $addr))
			$valid=false;
		}
		return $valid;
	}

	public function IsIP($addr)
	{
		if(!preg_match("/[^0-9\.]$/", $addr))
		return TRUE;
		else
		return FALSE;
	}

	public function IsEmail($eml)
	{	
		$valid = true;
		if($eml!='')
			if(preg_match("/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/",$eml) || !(preg_match("/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/",$eml)))
		$valid=false;
		return $valid;
	}

	public function IsNotEmpty($field)
	{
		if(trim($field)=="")
		return FALSE;
		else
		return TRUE;
	}

	public function IsSelected($field)
	{
		if(trim($field)=="0")
		return FALSE;
		else
		return TRUE;
	}

	public function IsNumber($field)
	{
		if(trim($field)!="")
		return is_numeric($field);
		else
		return true;
	}

	public function IsMoney($field)
	{
		$valid = true;
		$flen = strlen(trim($field));
		if($flen>0)
		{
			$allowed = "0123456789()-,.$";
			$digits = "0123456789";
			$numCount=0;
			for($i=0;$i<$flen;$i++)
			{
				if(!(strstr($allowed,substr($field,$i,1))))
				$valid=false;
				if(strstr($digits,substr($field,$i,1)))
				$numCount++;
			}
			if($valid && $numCount==0)
			$valid=false;
		}
		return $valid;
	}

	public function IsPercent($field)
	{
		$valid = true;
		$flen = strlen(trim($field));
		if($flen>0)
		{
			$allowed = "0123456789,.%";
			$digits = "0123456789";
			$numCount=0;
			for($i=0;$i<$flen;$i++)
			{
				if(!(strstr($allowed,substr($field,$i,1))))
				$valid=false;
				if(strstr($digits,substr($field,$i,1)))
				$numCount++;
			}
			if($valid && $numCount==0)
			$valid=false;
		}
		return $valid;
	}

	public function IsUSPhone($val)
	{
		$matchArr2 = preg_match("/^\d{10}$/",$val);
		$matchArr3 = preg_match("/^(\d{3})\-\d{3}-\d{4}$/",$val);
		$status = true;

		if ($matchArr2 == null && $matchArr3 == null && $this->IsNotEmpty($val))
		$status = false;
		return $status;
	}


	public function IsASTPhone($val)
	{
		$matchArr = preg_match("/^\d{8}$/",$val);
		$status = true;

		if ($matchArr == null && $this->IsNotEmpty($val))
		$status = false;

		return $status;
	}

	public function IsAreaCode($val)
	{
		$matchArr = preg_match("/^\d{2}$/",$val);
		$status = true;

		if ($matchArr == null && $this->IsNotEmpty($val))
		$status = false;

		return $status;
	}

	public function IsPostCode($val)
	{
		$matchArr = preg_match("/^\d{4}$/",$val);
		$status = true;

		if ($matchArr == null && $this->IsNotEmpty($val))
		$status = false;

		return $status;
	}

	public function IsColor($color)
	{
		$status = true;

		if($color != "")
		{
			if($color[0]=='#')
			{
				if(strlen($color)==7)
				{
					$color = "0x".substr($color,1,strlen($color));
					if(!is_numeric($color))
					$status = false;
				}
				else
				$status = false;
			}
		}
		else
		$status = false;

		return $status;
	}

	//Form field check functions
	public function checkCheckBox($field)
	{
		if(!isset($field))
		return 0;
		else
		return 1;
	}

	public function checkEmptyStr($field)
	{
		if($field=="")
		return 'NULL';
		else
		return "'".$field."'";
	}

	public function checkEmptyNum($field)
	{
		if($field=="")
		return 'NULL';
		else
		return $field;
	}

	public function strip_out($val)
	{
		$search = array(",","(",")","-","$","%"," ");
		$replace = array("","","","","","","");
		for($i=0;$i<sizeof($search);$i++)
		$val=str_replace($search[$i],$replace[$i],$val);
		return $val;
	}

}
?>